import wepy from '@wepy/core'
import { okwepy } from './okwepyheader'
import { StringEX } from '../MD/GMDWXMiniApp/StringEX';

//拨打电话
function callPhone(phone)
{
    wx.makePhoneCall({
        phoneNumber: phone
      });
}



function wxCallPhone(e)
{
    var phone = e.currentTarget.dataset.phone;
    callPhone(phone)
}

//复制到剪贴板
function setClipboardData(data,successTxt="复制成功")
{
    console.log("setClipboardData",data)
    wx.setClipboardData({
        data:String(data),
        success:()=>{
            wx.showToast({
                title: successTxt,
                icon: 'none',
                duration: 2000
            })
        }
    })
}

//输入框录入了信息
/*function onInput(e)
{
    var wxEvt = e.$wx
    var dataset = wxEvt.currentTarget.dataset
    var objname = dataset.obj?dataset.obj:"editData"
    var attrname = dataset.attr
    var obj = this[objname]
    obj[attrname] = wxEvt.detail.value

    console.log("onInput",attrname,wxEvt.detail.value)
}
*/
//输入框录入信息到数组中
function onInputArray(e)//arrName,idx,event
{ 
    var wxEvt = e.$wx
    var dataset = wxEvt.currentTarget.dataset
    var arrName = dataset.arr
    var idx = dataset.idx
    var isObj = dataset.isobj
    console.log("onInputArray",isObj)
    var arr = this[arrName]
    if(!arr)
    {
        console.log("onInputArray 目标不是数组")
        return
    } 

    if(idx>arr.length-1)
    {
        if(idx==arr.length)//刚好超过，追加到末尾
        {
            console.log("onInputArray 添加值",wxEvt.detail.value)


            arr.push(isObj?{id:StringEX.Guid(),nr:wxEvt.detail.value}:wxEvt.detail.value)
        }
        return
    }

    console.log("onInputArray 更新值",idx,wxEvt.detail.value)
    if(isObj)
        arr[idx] = {id:StringEX.Guid(),nr:wxEvt.detail.value}
    else
        arr[idx] = wxEvt.detail.value
}

var OnWepyMultipleInputSubmit = function(arr)
{
    console.log("OnMultipleInputSubmit",arr)     
    this.MultipleInputSubmitRecall(arr)
}

var OnMultipleInputSubmit = function(e)
{
    console.log("OnMultipleInputSubmit",e)
    var arr = e.detail.arguments[0]
    
    this.MultipleInputSubmitRecall(arr)
}

//title,placeholder,items,reCall
var ShowMultipleInput = function(
    obj
    )
{
    this.MultipleInputSubmitRecall = obj.reCall

    if(this.setData)
    { 
        this.setData({ MultipleInputVisible:false})
        this.setData(
            {
                MultipleInputVisible:true,
                MultipleInputTitle:obj.title,
                MultipleInputPlaceholder:obj.placeholder,
                MultipleInputItems:obj.items
            }
        )
    }
    else
    { 
        this.MultipleInputTitle=obj.title
        this.MultipleInputPlaceholder=obj.placeholder
        this.MultipleInputItems=obj.items
        this.MultipleInputVisible=false
        this.MultipleInputVisible=true
        
        
    }
}


//功能混入wepy组件或页面
okwepy.Mixins = function(obj)
{
    if(!obj.methods) obj.methods = {}
    obj.methods.onCallPhoneClick = callPhone
    obj.methods.onInputArray = onInputArray
    obj.methods.onCopy = setClipboardData
    //obj.methods.onInput = onInput
    obj.methods.OnMultipleInputSubmit = OnWepyMultipleInputSubmit
    obj.methods.ShowMultipleInput = ShowMultipleInput
    
    
    if(!obj.data) obj.data = {}
    obj.data.MultipleInputVisible = false
    obj.data.MultipleInputTitle = ""
    obj.data.MultipleInputPlaceholder = ""
    obj.data.MultipleInputItems = []
    
    return obj
}


 

//小程序原生页面
okwepy.wxpage = function(obj)
{
    if(!obj.data) obj.data = {}

    //多选输入弹窗功能
    obj.data.MultipleInputVisible = false

    obj.onCallPhoneClick = wxCallPhone
    obj.OnMultipleInputSubmit = OnMultipleInputSubmit
    obj.ShowMultipleInput = ShowMultipleInput

    Page(obj)
}